/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.resources.impl.internal;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;

import nemos.resources.IResources;

/**
 * ResourcesAdapter : The {@link ResourceBundle}/{@link IResources} adapter
 * class.
 * 
 * @author Romain DUBOIS
 * 
 * @param <T>
 *            The resources type
 */
public class ResourcesAdapter<T> implements IResources<T> {

    /** The adapted resources bundle */
    private final ResourceBundle mResources;
    /** The resources type */
    private final Class<? extends T> mInnerType;

    /**
     * Constructor
     * 
     * @param pResources
     *            The resources bundle to adapt
     * @param pType
     *            The resources type
     */
    public ResourcesAdapter(final ResourceBundle pResources, final Class<? extends T> pType) {
        super();

        mResources = pResources;
        mInnerType = pType;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.resources.IResources#containsKey(java.lang.String)
     */
    @Override
    public boolean containsKey(final String pKey) {
        return mResources.containsKey(pKey);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.resources.IResources#getLocale()
     */
    @Override
    public Locale getLocale() {
        return mResources.getLocale();
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.resources.IResources#keySet()
     */
    @Override
    public Set<String> keySet() {
        return mResources.keySet();
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.resources.IResources#getResource(java.lang.String)
     */
    @Override
    public T getResource(final String pKey) {
        try {
            return mInnerType.cast(mResources.getObject(pKey));
        } catch (final MissingResourceException e) {
            return null;
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.resources.IResources#adapt(java.lang.Class)
     */
    // Checked with the call to Class#isAssignableFrom(java.lang.Class)
    @SuppressWarnings("unchecked")
    @Override
    public <U> IResources<U> adapt(final Class<U> pNewType) {
        if (pNewType.isAssignableFrom(mInnerType)) {
            return (IResources<U>) this;
        }
        throw new ClassCastException(pNewType + " != " + mInnerType); //$NON-NLS-1$
    }
}
